package com.winhxd.b2c.common.feign.product;

import com.winhxd.b2c.common.constant.BusinessCode;
import com.winhxd.b2c.common.constant.ServiceName;
import com.winhxd.b2c.common.domain.ResponseResult;
import com.winhxd.b2c.common.domain.product.condition.ProductCategoryCondition;
import com.winhxd.b2c.common.domain.product.condition.ProductCategorySaveCondition;
import com.winhxd.b2c.common.domain.product.model.ProductCategory;
import com.winhxd.b2c.common.domain.product.vo.ProductCategoryVO;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author luosixiang
 */
@FeignClient(value = ServiceName.PRODUCT_SERVICE, fallbackFactory = ProductCategoryServiceFallback.class)
public interface ProductCategoryServiceClient {

	@RequestMapping(value = "/product/8200/v1/findProductCategory", method = RequestMethod.POST)
	ResponseResult<List<ProductCategoryVO>> findProductCategory();

	@RequestMapping(value = "/product/8202/v1/saveProductCategory", method = RequestMethod.POST)
	ResponseResult<List<ProductCategoryVO>> saveProductCategory(@RequestBody ProductCategorySaveCondition condition);

	@RequestMapping(value = "/product/8204/v1/checkAndDeleteProductCategory", method = RequestMethod.POST)
	ResponseResult<Boolean> checkAndDeleteProductCategory(@RequestBody ProductCategoryCondition condition);

	@RequestMapping(value = "/product/8206/v1/findProductCategoryByModel", method = RequestMethod.POST)
	ResponseResult<List<ProductCategory>> findProductCategoryByModel(@RequestBody ProductCategoryCondition condition);
}

@Component
class ProductCategoryServiceFallback implements ProductCategoryServiceClient, FallbackFactory<ProductCategoryServiceClient> {
    private static final Logger logger = LoggerFactory.getLogger(ProductCategoryServiceFallback.class);
    private Throwable throwable;

    public ProductCategoryServiceFallback(Throwable throwable) {
		this.throwable = throwable;
    }

    public ProductCategoryServiceFallback() {
    }


	@Override
	public ProductCategoryServiceClient create(Throwable throwable) {
		return new ProductCategoryServiceFallback(throwable);
	}

	@Override
	public ResponseResult<List<ProductCategoryVO>> findProductCategory() {
    	logger.error("查询商品分类接口调用失败,商品中心:8200");
		ResponseResult<List<ProductCategoryVO>> responseResult = new ResponseResult<>();
		responseResult.setCode(BusinessCode.CODE_1001);
		return responseResult;
	}

	@Override
	public ResponseResult<List<ProductCategoryVO>> saveProductCategory(ProductCategorySaveCondition condition) {
		logger.error("保存商品分类接口调用失败,商品中心:8202");
		ResponseResult<List<ProductCategoryVO>> responseResult = new ResponseResult<>();
		responseResult.setCode(BusinessCode.CODE_1001);
		return responseResult;
	}

	@Override
	public ResponseResult<Boolean> checkAndDeleteProductCategory(ProductCategoryCondition condition) {
		logger.error("判断商品分类能否删除接口调用失败,商品中心:8204");
		ResponseResult<Boolean> responseResult = new ResponseResult<>();
		responseResult.setCode(BusinessCode.CODE_1001);
		return responseResult;
	}

	@Override
	public ResponseResult<List<ProductCategory>> findProductCategoryByModel(ProductCategoryCondition condition) {
		logger.error("根据productCategory查询接口调用失败,商品中心:8206");
		ResponseResult<List<ProductCategory>> responseResult = new ResponseResult<>();
		responseResult.setCode(BusinessCode.CODE_1001);
		return responseResult;
	}
}
